package com.cskaoyan.servlet;

import com.cskaoyan.bean.MarketCategory;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.vo.CategoryVo;
import com.cskaoyan.service.CategoryService;
import com.cskaoyan.service.CategoryServiceimpl;
import com.cskaoyan.util.JacksonUtil;
import com.cskaoyan.util.ResponseUtil;
import lombok.SneakyThrows;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Ramsey
 * @date 2023年05月31日 19:02
 */
@WebServlet("/admin/category/*")
public class AdminCategoryServlet extends WdBaseServlet {
    CategoryService categoryService = new CategoryServiceimpl();

    // @SneakyThrows
    // public void list(HttpServletRequest request, HttpServletResponse response) {
    //     List<MarketCategory> marketCategories = categoryService.selectAll();
    //     CommonData data = CommonData.data(marketCategories);
    //     ResponseUtil.responseJson(response, BaseRespVo.ok(data));
    //     //2023年5月31日19:12:12 这里是有问题的。为了让浏览器先不报错
    //     //没有做到一二级目录的关系展示
    // }

    //新增的
    private void list(HttpServletRequest req, HttpServletResponse response) throws IOException {
        List<CategoryVo> categoryVoList = new ArrayList<>();

        List<MarketCategory> categoryList = categoryService.queryBypid(0);

        for (MarketCategory L1 : categoryList) {
            CategoryVo categoryVo = new CategoryVo();
            categoryVo.setId(L1.getId());
            categoryVo.setName(L1.getName());
            categoryVo.setKeywords(L1.getKeywords());
            categoryVo.setDesc(L1.getDesc());
            categoryVo.setIconUrl(L1.getIconUrl());
            categoryVo.setPicUrl(L1.getPicUrl());
            categoryVo.setLevel(L1.getLevel());

            List<CategoryVo> children = new ArrayList<>();
            List<MarketCategory> subCategoryList = categoryService.queryBypid(L1.getId());
            for (MarketCategory L2vo : subCategoryList) {
                CategoryVo categoryVo2 = new CategoryVo();
                categoryVo2.setId(L2vo.getId());
                categoryVo2.setName(L2vo.getName());
                categoryVo2.setKeywords(L2vo.getKeywords());
                categoryVo2.setDesc(L2vo.getDesc());
                categoryVo2.setIconUrl(L2vo.getIconUrl());
                categoryVo2.setPicUrl(L2vo.getPicUrl());
                categoryVo2.setLevel(L2vo.getLevel());
                children.add(categoryVo2);
            }
            categoryVo.setChildren(children);
            categoryVoList.add(categoryVo);
        }
        ResponseUtil.responseJson(response, BaseRespVo.ok(CommonData.data(categoryVoList)));
    }


    private void l1(HttpServletRequest req, HttpServletResponse response) throws IOException {
        List<MarketCategory> l1List = categoryService.queryL1();
        List<Map<String, Object>> data = new ArrayList<>(l1List.size());
        for (MarketCategory category : l1List) {
            Map<String, Object> d = new HashMap<>(2);
            d.put("value", category.getId());
            d.put("label", category.getName());
            data.add(d);
        }
        ResponseUtil.responseJson(response, BaseRespVo.ok(CommonData.data(data)));
    }

    //  /admin/category/create
    private void create(HttpServletRequest req, HttpServletResponse response) throws IOException {
        String jsonStr = req.getReader().readLine();
        MarketCategory category = JacksonUtil.read(jsonStr, MarketCategory.class);
        BaseRespVo baseRespVo = categoryService.create(category);
        ResponseUtil.responseJson(response, baseRespVo);
    }


    //  /admin/category/update
    private void update(HttpServletRequest req, HttpServletResponse response) throws IOException {
        String jsonStr = req.getReader().readLine();
        MarketCategory category = JacksonUtil.read(jsonStr, MarketCategory.class);
        BaseRespVo baseRespVo = categoryService.update(category);
        ResponseUtil.responseJson(response, baseRespVo);

    }

    //  /admin/category/delete
    private void delete(HttpServletRequest req, HttpServletResponse response) throws IOException {
        String jsonStr = req.getReader().readLine();
        MarketCategory category = JacksonUtil.read(jsonStr, MarketCategory.class);
        BaseRespVo baseRespVo = categoryService.delete(category);
        ResponseUtil.responseJson(response, baseRespVo);
    }

}
